package by.grsu.shedule;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import by.grsu.shedule.constants.ApplicationConstants;

public class SettingsActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.preferences);
		setSummary(ApplicationConstants.COURSE_PREFERENCES,
				R.string.cource_not_selected);
		setSummary(ApplicationConstants.GROUP_PREFERENCES,
				R.string.group_not_selected);
		setSummary(ApplicationConstants.LANGUAGE_LIST_PREFERENCE,
				R.string.translit);

	}

	@Override
	protected void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);

	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals(ApplicationConstants.COURSE_PREFERENCES)) {
			setSummary(ApplicationConstants.COURSE_PREFERENCES,
					R.string.cource_not_selected);
		}

		if (key.equals(ApplicationConstants.GROUP_PREFERENCES)) {
			setSummary(ApplicationConstants.GROUP_PREFERENCES,
					R.string.group_not_selected);
		}

		if (key.equals(ApplicationConstants.LANGUAGE_LIST_PREFERENCE)) {
			setSummary(ApplicationConstants.LANGUAGE_LIST_PREFERENCE,
					R.string.translit);
		}

	}

	private void setSummary(String key, int defValue) {
		getPreferenceScreen().findPreference(key).setSummary(
				PreferenceManager.getDefaultSharedPreferences(
						getApplicationContext()).getString(key,
						getResources().getString(defValue)));
	}
}
